/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.service.impl;

import com.dejax.domain.DevopsWorkorderdetail;
import com.dejax.utils.ValidationUtil;
import com.dejax.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.dejax.repository.DevopsWorkorderdetailRepository;
import com.dejax.service.DevopsWorkorderdetailService;
import com.dejax.service.dto.DevopsWorkorderdetailDto;
import com.dejax.service.dto.DevopsWorkorderdetailQueryCriteria;
import com.dejax.service.mapstruct.DevopsWorkorderdetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.dejax.utils.PageUtil;
import com.dejax.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Richer
* @date 2022-01-07
**/
@Service
@RequiredArgsConstructor
public class DevopsWorkorderdetailServiceImpl implements DevopsWorkorderdetailService {

    private final DevopsWorkorderdetailRepository devopsWorkorderdetailRepository;
    private final DevopsWorkorderdetailMapper devopsWorkorderdetailMapper;

    @Override
    public Map<String,Object> queryAll(DevopsWorkorderdetailQueryCriteria criteria, Pageable pageable){
        criteria.setFlag(0);
        Page<DevopsWorkorderdetail> page = devopsWorkorderdetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(devopsWorkorderdetailMapper::toDto));
    }

    @Override
    public List<DevopsWorkorderdetailDto> queryAll(DevopsWorkorderdetailQueryCriteria criteria){
        criteria.setFlag(0);
        return devopsWorkorderdetailMapper.toDto(devopsWorkorderdetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DevopsWorkorderdetailDto findById(String workorderdetailid) {
        DevopsWorkorderdetail devopsWorkorderdetail = devopsWorkorderdetailRepository.findById(workorderdetailid).orElseGet(DevopsWorkorderdetail::new);
        ValidationUtil.isNull(devopsWorkorderdetail.getWorkorderdetailid(),"DevopsWorkorderdetail","workorderdetailid",workorderdetailid);
        return devopsWorkorderdetailMapper.toDto(devopsWorkorderdetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DevopsWorkorderdetailDto create(DevopsWorkorderdetail resources) {
        resources.setWorkorderdetailid(IdUtil.simpleUUID());
        resources.setFlag(0);
        return devopsWorkorderdetailMapper.toDto(devopsWorkorderdetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DevopsWorkorderdetail resources) {
        DevopsWorkorderdetail devopsWorkorderdetail = devopsWorkorderdetailRepository.findById(resources.getWorkorderdetailid()).orElseGet(DevopsWorkorderdetail::new);
        ValidationUtil.isNull( devopsWorkorderdetail.getWorkorderdetailid(),"DevopsWorkorderdetail","id",resources.getWorkorderdetailid());
        devopsWorkorderdetail.copy(resources);
        devopsWorkorderdetailRepository.save(devopsWorkorderdetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String workorderdetailid : ids) {
            //devopsWorkorderdetailRepository.deleteById(workorderdetailid);
        }
    }

    @Override
    public void download(List<DevopsWorkorderdetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DevopsWorkorderdetailDto devopsWorkorderdetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" workorderid",  devopsWorkorderdetail.getWorkorderid());
            map.put("操作详情", devopsWorkorderdetail.getOperationid());
            map.put("操作参数", devopsWorkorderdetail.getOperateparams());
            map.put("执行结果", devopsWorkorderdetail.getOperateresult());
            map.put("操作时间", devopsWorkorderdetail.getOperatetime());
            map.put(" flag",  devopsWorkorderdetail.getFlag());
            map.put(" notes",  devopsWorkorderdetail.getNotes());
            map.put(" reguser",  devopsWorkorderdetail.getReguser());
            map.put(" regtime",  devopsWorkorderdetail.getRegtime());
            map.put(" moduser",  devopsWorkorderdetail.getModuser());
            map.put(" modtime",  devopsWorkorderdetail.getModtime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}